import {Component, OnInit} from '@angular/core';
import {HttpService} from '../../service/http.service';
import {ActivatedRoute, Params} from '@angular/router';
import 'rxjs/add/operator/switchMap';
import {Book} from '../../module/book';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

  keyword: string;
  bookList: Book[];
  resultCount: number;

  constructor(private route: ActivatedRoute,
              private httpService: HttpService) {
  }

  ngOnInit() {
    this.route.params
      .subscribe(params => {
        this.keyword = params['key'];
        this.httpService.searchBook(this.keyword).then(r => {
          this.bookList = r;
          this.resultCount = r.length;
          console.log(this.bookList);
        });
      });
  }

}
